package com.example.security.bean;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

public class UserInfo implements UserDetails {

    private static final long serialVersionUID = 3740751765229566840L;

    /**
     * 主键ID
     */
    private Integer uid;
    /**
     * 帐号
     */
    private String userName;
    /**
     * 名称（昵称或者真实姓名，不同系统不同定义）
     */
    private String name;
    /**
     * 密码;
     */
    private String password;

    /**
     * 用户状态,0:创建未认证（比如没有激活，没有输入验证码等等）--等待验证的用户 , 1:正常状态,2：用户被锁定.
     */
    private int state;

    private List<SysRole> sysRoleList;


    public Integer getUid() {
        return uid;
    }

    public void setUid(Integer uid) {
        this.uid = uid;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<SysRole> getSysRoleList() {
        return sysRoleList;
    }

    public void setSysRoleList(List<SysRole> sysRoleList) {
        this.sysRoleList = sysRoleList;
    }

    /**
     * 获取授权
     * @return
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
        /**
         * 加载用户所有的角色
         */
        sysRoleList.forEach((role)->{
            authorities.add(new SimpleGrantedAuthority(role.getRole()));
        });
        return authorities;
    }

    @Override
    public String getUsername() {
        return this.userName;
    }

    /**
     * 未过期
     * @return
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 是否未锁定
     * @return
     */
    @Override
    public boolean isAccountNonLocked() {
        return state == 0;
    }

    /**
     * 凭证是否到期
     * @return
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 是否禁止登录
     * @return
     */
    @Override
    public boolean isEnabled() {
        return true;
    }


    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }


    @Override
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "UserInfo{" +
                "uid=" + uid +
                ", userName='" + userName + '\'' +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                ", state=" + state +
                ", sysRoleList=" + sysRoleList +
                '}';
    }

    /**
     * 重写equals 和hashcode 方法，实现踢出用户
     * @param o
     * @return
     */
    @Override
    public boolean equals(Object o){
        if(this == o) {
            return  true;
        }
        if(o ==null ||getClass()!=o.getClass() ){
            return false;
        }
        UserInfo userInfo=(UserInfo) o;
        return Objects.equals(userName,userInfo.userName);
    }
    @Override
    public int hashCode( ){
        return Objects.hash(userName);
    }
}